package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class CommentsActivity extends Activity {

    private List<Map<String, Object>> mListItems;
    private ListView mCommentsList;
    private CommentsListViewAdapter mCommentsAdapter;

    private NetworkImpl mCommentsListNetwork;

    private TextView mTitle;
    private ViewGroup mMyComments;

    private String[] mCommentsNick;
    private String[] mCommentsDate;
    private String[] mComments;
    private Bitmap[] mCommentsIcon;

    private String mAppId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_comments);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.arrow_left);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);

        title1.setPadding(width/3, 0, 0, 0);
        title1.setText(R.string.comment);

        tmp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                CommentsActivity.this.finish();
            }

        });

        mAppId = getIntent().getExtras().getString("appid");

        String description = String.format(
                getResources().getString(R.string.comments_number_total)
              , "0");

        mTitle = (TextView)findViewById(R.id.comments_counter);
        mTitle.setText(description);

        mMyComments = (ViewGroup)findViewById(R.id.comments_my_comment);
        mMyComments.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent tmp = new Intent(CommentsActivity.this, MyCommentsActivity.class);
                tmp.putExtra("appid", mAppId);
                startActivity(tmp);
            }

        });

        mCommentsList = (ListView)findViewById(R.id.comments_lists);

        mCommentsListNetwork = new NetworkImpl(this, new MyNetworkCallBack());
        mCommentsListNetwork.getAppComments("123", mAppId, 10);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.comments, menu);
        return true;
    }

    private List<Map<String, Object>> getListItems() {
        List<Map<String, Object>> innnerListItems = new ArrayList<Map<String, Object>>();
        if (mCommentsNick != null) {
            for(int i = 0; i < mCommentsNick.length; i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("nick", mCommentsNick[i]);
                map.put("date", mCommentsDate[i]);
                map.put("comment", mComments[i]);
                map.put("icon", mCommentsIcon[i]);
                innnerListItems.add(map);
            }
        }
        return innnerListItems;
    }

    private class CommentsListViewAdapter extends BaseAdapter {

        private List<Map<String, Object>> listItems;
        private LayoutInflater listContainer;

        public final class ListItemView{
            public ImageView icon;
            public TextView nick;
            public TextView date;
            public TextView comment;
        }

        public CommentsListViewAdapter(Context context, List<Map<String, Object>> listItems) {
            listContainer = LayoutInflater.from(context);
            this.listItems = listItems;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listItems.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return listItems.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ListItemView  listItemView = null;
            if (convertView == null) {
                listItemView = new ListItemView();
                convertView = listContainer.inflate(R.layout.comments_list_item, null);

                listItemView.icon = (ImageView)convertView.findViewById(R.id.comments_item_icon);
                listItemView.nick = (TextView)convertView.findViewById(R.id.comments_item_nick);
                listItemView.date = (TextView)convertView.findViewById(R.id.comments_item_date);
                listItemView.comment = (TextView)convertView.findViewById(R.id.comments_item_comment);
                convertView.setTag(listItemView);
            }else {
                listItemView = (ListItemView)convertView.getTag();
            }

            final String nick = (String)listItems.get(position).get("nick");
            final String date  = (String)listItems.get(position).get("date");
            final String comment  = (String)listItems.get(position).get("comment");
            final Bitmap icon = (Bitmap)listItems.get(position).get("icon");

            listItemView.nick.setText(nick);
            listItemView.date.setText(date + getResources().getString(R.string.comments_intern));
            listItemView.comment.setText(comment);
            listItemView.icon.setImageBitmap(icon);

            return convertView;
        }

    }

    class MyNetworkCallBack extends NetworkCallBack {
        @SuppressWarnings("unchecked")
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(CommentsActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_GET_APP_COMMENTS:
                mListItems = (List<Map<String, Object>>)attach1;
                String description = String.format(
                        getResources().getString(R.string.comments_number_total)
                      , mListItems.size());
                mTitle.setText(description);
                mCommentsAdapter = new CommentsListViewAdapter(CommentsActivity.this, mListItems);
                mCommentsList.setAdapter(mCommentsAdapter);
                break;
            }
        }
    }

}
